<?php
// 使用Composer autoloader引入SDK
date_default_timezone_set('America/Los_Angeles');
require 'vendor/autoload.php';

class Client {
    var $host;
    var $access_key;
    var $secret_key;

    public function __construct($host,$access_key,$secret_key){
        $this->host=$host;
        $this->access_key=$access_key;
        $this->secret_key=$secret_key;
        $this->conn();
    }

    public $minioClient;

    public function conn(){
        $this->minioClient = new Aws\S3\S3Client([
                'version'=>'latest',
                'region'=>'us-east-1',
                'endpoint'=>$this->host,
                'use_path_style_endpoint'=>true,
                'credentials'=>[
                'key'=>$this->access_key,
                'secret'=>$this->secret_key
            ]
        ]);
    }


    # 创建一个存储桶
    public function createBucket($bucket){
        return $this->minioClient->createBucket([
            'Bucket' => $bucket
        ]);
    }

    # 检查存储桶是否存在
    public function headBucket($bucket){
        try {
            return $this->minioClient->headBucket(['Bucket' => $bucket]);
        } catch (\Throwable $th) {
            //throw $th;
            return false;
        }
       
    }

    # 桶列表
    public function listBuckets(){
        return $this->minioClient->listBuckets();
    }

    # 添加一个新的对象到对象存储服务。
    public function putObject($bucket,$body,$file_path){
        $ContentTypestr = ContentType($file_path);
        return $this->minioClient->putObject([
            'Body' => $body,
            'Bucket' => $bucket,
            'Key' => $file_path,
            'ContentType'=>$ContentTypestr
    ]);
    }

    # 删除对象
    public function deleteObject($bucket,$file_path){
        return $this->minioClient->deleteObject([
            'Bucket' => $bucket,
            'Key' => $file_path
        ]);
    }

    # 生成一个用于HTTP GET操作的presigned URL presigned_get_object
    public function getObjectUrl($bucket,$object_name){
        return $this->minioClient->getObjectUrl($bucket,$object_name);
    }
    
}

function ContentType($file){
    $houzui = substr(strrchr($file, '.'), 1);
    switch($houzui){
        case 'bmp':
            return "application/x-bmp";break;
        case 'gif':
            return "image/gif";break;
        case 'jpe':
            return "image/jpeg";break;
        case 'jpeg':
            return "image/jpeg";break;
        case 'jpg':
            return "image/jpeg";break;
        case 'png':
            return "image/png";break;
        case 'tif':
            return "image/tiff";break;
        case 'jfif':
            return "image/jpeg";break;
        case 'tiff':
            return "image/tiff";break;
        default:
            return "application/octet-stream";
            
    }
}

// $host = 'http://192.168.1.108:9000';
// $access_key = 'ILE2HBZKM9N3OVJSYZUQ';
// $secret_key = 'CM2aq4iVlI9tzZOeRquqmTEDaVhKhQOM7+hSjaJx';
// $minioClient = new Client($host,$access_key,$secret_key);

#$minioClient->createBucket('snsnb'); # 创建一个存储桶
// $result = $minioClient->headBucket('discuz123'); # 检查存储桶是否存在
# $result = $minioClient->listBuckets(); # 列出所有的存储桶


// 添加一个新的对象到对象存储服务。 put_object
// $body = file_get_contents('pic_12.jpg');
// $result = $minioClient->putObject('discuz',$body,'2023/pic_12.jpg');
// 通过文件上传到对象中 fput_object
// 获取对象信息 stat_object
// 生成一个用于HTTP GET操作的presigned URL presigned_get_object
# $result = $minioClient->getObjectUrl('discuz','2022/ask_banner_m123.jpg'); # 列出所有的存储桶
// print_r($result);
// var_dump($result);